%*************************************************************************************** 
%Firm-embedded productivity and cross-country income differences
%Alviarez, Cravino and Ramondo
%Journal of Political Economy (2022)

%Program: model_07_selection.do
%Date: October 2022

%Description: Constructing bins of to check for the additive separability assumption 

%*****************************************************************************************/

cd '.../dataverse_files/data/analysis'

T = readtable('selection_tomatlab_type_10_agg_woparent.csv'); %Imports the file from Stata
agg_data = table2array(T);   % Extract Table Data To Double Array
agg_data2=agg_data(:,2:end);
dd=10;
x=size(agg_data2,2)/dd;
x
agg_data3=reshape(agg_data2, dd,dd,x); 
BaseName='graphs_additivity_type_decile_agg_woparent_g';


%----------------------------------------------------------------
%OLS residuals by MNE-sector and market popularity
%----------------------------------------------------------------
kk=2; 
base=kk;
agg_data4=agg_data3(:,:,base); 

X_country=agg_data4(:,1); 
Y_guoFE=agg_data4(:,2); 
Z_error=agg_data4(:,3); 

figure
grid on 
grid minor
b=bar3(agg_data4,'detached');
set(gca,'fontsize',8, 'YTick',[1,2,3,4,5,6,7,8,9,10],'yticklabel',{'1'; '2'; '3'; '4'; '5'; '6'; '7'; '8'; '9'; '10'});
set(gca,'fontsize',8, 'XTick',[1,2,3,4,5,6,7,8,9,10],'xticklabel',{'1'; '2'; '3'; '4'; '5'; '6'; '7'; '8'; '9'; '10'});
set(gca,'fontsize',8, 'ZTick',(-0.5:0.1:0.5));
ax = gca;
ax.YLabel.String = 'Market Popularity Decile'; 

ax.YLabel.FontSize = 12;
ax.XLabel.String = 'MNE-sector FE Decile';

ax.XLabel.FontSize = 12;
zlabel('Mean standardized $\epsilon_{in}(\omega)$','Interpreter','latex'); 
ax.ZLabel.FontSize = 12;
ytickangle(0)
zlim([-0.5 0.5])
colormap('winter');
for k = 1:length(b)
    zdata = b(k).ZData;
    b(k).CData = zdata;
    b(k).FaceColor = 'interp';
end
view(48, 22)

FileName0='Figure_7a';
FileName1 = fullfile('.../dataverse_files/results/main', FileName0);
print('-bestfit',FileName1,'-dpdf')
close(gcf)


%----------------------------------------------------------------
%OLS residuals by MNE-sector and country-embedded factors
%----------------------------------------------------------------
kk=1; 
kk
base=kk
agg_data4=agg_data3(:,:,base); 

X_country=agg_data4(:,1); 
Y_guoFE=agg_data4(:,2); 
Z_error=agg_data4(:,3); 

figure
grid on 
grid minor
b=bar3(agg_data4,'detached');
set(gca,'fontsize',8, 'YTick',[1,2,3,4,5,6,7,8,9,10],'yticklabel',{'1'; '2'; '3'; '4'; '5'; '6'; '7'; '8'; '9'; '10'});
set(gca,'fontsize',8, 'XTick',[1,2,3,4,5,6,7,8,9,10],'xticklabel',{'1'; '2'; '3'; '4'; '5'; '6'; '7'; '8'; '9'; '10'});
set(gca,'fontsize',8, 'ZTick',(-0.5:0.1:0.5));
ax = gca;
ax.YLabel.String = 'Country-Embedded Factor Decile';
ax.YLabel.FontSize = 12;
ax.XLabel.String = 'MNE-sector FE Decile';

ax.XLabel.FontSize = 12;
zlabel('Mean standardized $\epsilon_{in}(\omega)$','Interpreter','latex'); 
ax.ZLabel.FontSize = 12;
ytickangle(0)
zlim([-0.5 0.5])
colormap('winter');
for k = 1:length(b)
    zdata = b(k).ZData;
    b(k).CData = zdata;
    b(k).FaceColor = 'interp';
end
view(48, 22)

FileName0='Figure_7b';
FileName1 = fullfile('.../dataverse_files/results/main', FileName0);
print('-bestfit',FileName1,'-dpdf')
close(gcf)


